package ttsolver.variables;

import ttsolver.input.Node;
import ttsolver.input.Train;

public class TWTcalc extends DefaultVariable {

	private Train train;
	private int signal; // 0 - minus

	// 1 - plus

	public TWTcalc() {
		super(VariableType.TWT);
	}

	public TWTcalc(Train train, int signal) {
		super(VariableType.TWT);
		this.train = train;
		this.signal = signal;
	}

	public Train getTrain() {
		return train;
	}

	public void setTrain(Train train) {
		this.train = train;
	}

	public int getSignal() {
		return signal;
	}

	public void setSignal(int signal) {
		this.signal = signal;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((train == null) ? 0 : train.getId().intValue());
		result = prime * result + signal;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;

		TWTcalc other = (TWTcalc) obj;

		if (train == null) {
			if (other.train != null)
				return false;
		} else if (!train.equals(other.train)) {
			return false;
		}

		if (signal != other.signal) {
			return false;
		}
		
		return true;
	}

	@Override
	public int compareTo(Object obj) {
		final int BEFORE = -1;
		final int EQUAL = 0;
		final int AFTER = 1;

		TWTcalc other = (TWTcalc) obj;

		if (this == other)
			return EQUAL;

		if (this.train != null && other.train != null) {
			if (this.train.getId() < other.train.getId())
				return BEFORE;
			if (this.train.getId() > other.train.getId())
				return AFTER;
		}

		if (this.signal < other.signal)
			return BEFORE;
		if (this.signal > other.signal)
			return AFTER;

		return EQUAL;
	}

	@Override
	public String toString() {
		if (!printVar)
			return "";

		String s = (train != null) ? String.valueOf(train.getId()) : "";
		if(signal == 0)
			return "Twt_-_(" + s + ")";
		else
			return "Twt_+_(" + s + ")";
		
	}
}
